package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 冰白寒祭
 * @date 2023-09-04 16:11:21
 * @description 购物车相关
 */
@Service
@Slf4j

public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * @param shoppingCartDTO
     * @MethodName: addShoppingCart
     * @Author 冰白寒祭
     * @Description: 添加购物车
     * @Date: 2023-09-04 16:09:35
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        // 判断当前加入到购物车中的商品是否已经存在了

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        // 如果已经存在了,只需要将数量加1
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();


        if (list != null && list.size() > 0) {
            ShoppingCart cart = list.get(0);


            if (cart.getDishId() != null) {
                // 菜品价格更新
                Dish dish = dishMapper.getById(dishId);
                cart.setAmount(cart.getAmount().add(dish.getPrice()));
            } else {
                // 套餐价格更新
                Setmeal setmeal = setmealMapper.getById(setmealId);
                cart.setAmount(cart.getAmount().add(setmeal.getPrice()));
            }

            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.upDateNumberById(cart);
        } else {
            // 如果不存在,需要插入一条购物车数据
            // 判断是菜品还是套餐

            if (dishId != null) {
                // 是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());

                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 是套餐
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * @MethodName: showShoppingCart
     * @Author 冰白寒祭
     * @Description: 查看购物车
     * @Date: 2023-09-04 19:45:15
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        // 获取当前微信用户id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * @MethodName: cleanShoppingCart
     * @Author 冰白寒祭
     * @Description: 清空购物车
     * @Date: 2023-09-04 20:14:50
     */
    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }
}
